package com.sky.utils;

import com.alibaba.fastjson.JSONObject;
import com.sky.exception.OrderBusinessException;
import com.sky.properties.WeChatProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
@Slf4j
public class MockWeChatPayUtil {
    @Autowired
    private WeChatProperties weChatProperties;
    public JSONObject pay(String orderNumber, BigDecimal amount, String description, String openid) {
        // 模拟微信支付返回的数据结构
        JSONObject jsonObject = new JSONObject();

        // 模拟支付成功返回
        jsonObject.put("code", "SUCCESS");
        jsonObject.put("message", "OK");

        // 模拟预支付交易会话标识
        jsonObject.put("prepay_id", "wx" + UUID.randomUUID().toString().replace("-", ""));

        // 模拟package参数
        Map<String, String> packageMap = new HashMap<>();
        packageMap.put("appId", weChatProperties.getAppid());
        packageMap.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
        packageMap.put("nonceStr", UUID.randomUUID().toString().replace("-", ""));
        packageMap.put("package", "prepay_id=" + jsonObject.getString("prepay_id"));
        packageMap.put("signType", "RSA");
        // 这里signature可以模拟一个假的签名
        packageMap.put("paySign", "MOCK_SIGNATURE_" + UUID.randomUUID().toString().replace("-", ""));

        jsonObject.put("package", packageMap);

        return jsonObject;
    }
    /**
     * 模拟微信支付退款
     * @param outTradeNo 商户订单号
     * @param outRefundNo 商户退款单号
     * @param refund 退款金额
     * @param total 原订单金额
     * @return 模拟退款结果
     */
    public JSONObject refund(String outTradeNo, String outRefundNo, BigDecimal refund, BigDecimal total) {
        log.info("模拟微信退款：订单号={}, 退款单号={}, 退款金额={}, 原金额={}",
                outTradeNo, outRefundNo, refund, total);

        // 模拟退款处理时间
        try {
            Thread.sleep(1000); // 模拟1秒处理时间
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        // 构造模拟退款响应数据
        JSONObject result = new JSONObject();
        result.put("status", "SUCCESS");
        result.put("code", "SUCCESS");
        result.put("message", "模拟退款成功");
        result.put("out_trade_no", outTradeNo);
        result.put("out_refund_no", outRefundNo);
        result.put("refund_id", "MOCK_REFUND_" + UUID.randomUUID().toString().replace("-", ""));
        result.put("refund_fee", refund);
        result.put("total_fee", total);

        return result;
    }
}